<template>
	<view class="layout">123
	<slot></slot>
	</view>
</template>

<script>
	import { createModalComp, createToastComp } from "./utils/comp";
	import { queryConfig } from "./service/index";
	import { checkHasLogined , login} from "./utils/service";
	export default {
		globalConfig: {
			isConnected: true
		}, // 全局配置
		
		onLaunch: function() {
			const miniPrograme = uni.getAccountInfoSync().miniProgram;
			uni.setStorageSync("appid", miniPrograme.appId);
			uni.setStorageSync('envVersion', miniPrograme.envVersion);
			uni.setStorageSync('version', miniPrograme.version);
			
			// 检测新版本
			const updateManager = uni.getUpdateManager() // 基础 - 更新  更新管理器
			updateManager.onUpdateReady(function () { // 管理器函数，更新相关
				createModalComp({
					title: "更新提示",
					content: "新版本已经准备好，是否重启应用？",
					success: (res) => {
						if (res.confirm)  updateManager.applyUpdate();
					}
				});
			})
			
			/**
			 * 初次加载判断网络情况
			 */
			uni.getNetworkType({ // 设备 -网络
				success(res) {
					const networkType = res.networkType
					if (networkType === 'none') {
						getApp().globalData.isConnected = false;
						createToastComp({ title: '当前无网络' }, "loading");
					}
				},
				fail() {
					createToastComp({ title: '服务接口错误' }, "loading");
				}
			});

			/**
			 * 监听网络状态变化
			 */
			uni.onNetworkStatusChange(function(res) {
				if (!res.isConnected) {
					getApp().globalData.isConnected = false;
					createToastComp({ title: '网络已断开' }, "loading");
				} else {
					getApp().globalData.isConnected = true
				}
			})

			// 获取一些全局的配置放在storage中
			queryConfig("dangxiajing").then(res => {
				if (res.status === "success") {
					res.data?.forEach(item => {
						uni.setStorageSync(item.config_key, item.value);
					});
					// this.configLoadOK?.();
				}
			})
			console.log('App Launch');
		},

		onShow: function() {
			checkHasLogined().then(isLogined => {
				if (!isLogined) {
					login();
				} else {
					uni.switchTab({
					  url: '/pages/index/index'
					})
				}
			})
			console.log('App Show');
		},

		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
/*每个页面公共css */
page {
	font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica,
		Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 'Microsoft Yahei',
		sans-serif;
}
</style>
